package data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class TraceMapHandler extends DefaultHandler{
	
	  private static final int ALL_CLOSED = 0;
	  private static final int OPENED_TRKPT = 1;
	  private static final int OPENED_ELE = 2;
	  private static final int OPENED_TIME= 3;
	  
	  private boolean multipleTracks = false;
  
	  private TraceMap tm;
	  
	  private Vector<TraceMap> vtm;
	  
	  private Location cl;
	  
	  private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	  
	  private Date t;
	  
	  private int status = ALL_CLOSED;
	  
	  
	
	  public TraceMapHandler(TraceMap tm){
		  this.tm = tm;
		  this.multipleTracks=false;
	  }
	
	  public TraceMapHandler(Vector<TraceMap> vtm){
		  this.vtm = vtm;
		  this.tm = new TraceMap();
		  this.multipleTracks = true;
	  }
	
	  @Override 
	  public void startDocument() 
	  { 
	    System.out.println( "Document starts." ); 
	    cl = new Location (0, 0, 0);
	    Date t = new Date();
	  } 
	 
	  @Override 
	  public void endDocument() 
	  { 
	    System.out.println( "Document ends." ); 
	  }
	  @Override 
	  public void startElement( String namespaceURI, String localName, 
	                            String qName, Attributes atts ) 
	  { 
		if ("trk".equals(qName) && multipleTracks){
			  this.tm = new TraceMap();
		}
		  
	    if ("time".equals(qName) && status == OPENED_TRKPT){
	    	status = OPENED_TIME;
	    }
	    if ("ele".equals(qName) && status == OPENED_TRKPT){
	    	status = OPENED_ELE;
	    }
	    if ("trkpt".equals(qName) && status == ALL_CLOSED){
	    	status = OPENED_TRKPT;
		    cl = new Location();
		    t = new Date();
		      for ( int i = 0; i < atts.getLength(); i++ ) {
		    	if ("lon".equals(atts.getQName( i ))){
		    		cl.setLongitude(Float.parseFloat(atts.getValue( i )));
		    	}
		    	else if ("lat".equals(atts.getQName( i ))){
		    		cl.setLatitude(Float.parseFloat(atts.getValue( i )));
		    	}
		      }
	    }
	 
	  }
	  @Override
	  public void endElement(String namespaceURI, String localName, String qName){
			if ("trk".equals(qName) && multipleTracks){
				  this.vtm.add(tm);
			}

			if ("trkpt".equals(qName) && status != ALL_CLOSED){
		    	tm.put(t, cl);
		    	status = ALL_CLOSED;
		    }
		    if (("time".equals(qName) || "ele".equals(qName) )&& status != ALL_CLOSED){
		    	status = OPENED_TRKPT;
		    }
		    
		  
	  }
	  @Override 
	  public void characters( char[] ch, int start, int length ) 
	  { 
	 
	    StringBuffer sb = new StringBuffer();
	    
	    
	    for ( int i = start; i < (start + length); i++ ) 
	      sb.append(ch[i]); 
	 
	    String str = sb.toString();
	    
	    if (status == OPENED_TIME){
	    	try {
				t = sdf.parse(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(t);
	    }
	    if (status == OPENED_ELE){
	    	cl.setElevation(Float.parseFloat(str));
	    }
	    
//	    System.out.println(str); 
	  } 
	
}
